home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 8171 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.7 KB  |  56 lines

  1. Path: pubxfer.news.psi.net!usenet
  2. From: lif@lutz.com
  3. Newsgroups: comp.lang.pascal.misc,comp.lang.c++,comp.lang.c,comp.lang.pascal.borland
  4. Subject: Re: Tough FACTORIAL math problem...
  5. Date: Thu, 15 Feb 1996 12:44:36 GMT
  6. Message-ID: <4fv9m9$h7f@client1.news.psi.net>
  7. References: <4fr8be$ass@news.iconn.net>
  8. NNTP-Posting-Host: 206.0.7.4
  9. X-Newsreader: Forte Free Agent 1.0.82
  10.  
  11. thecrow@iconn.net (The Crow) wrote:
  12.  
  13. >Here is what I am trying to do, I want to find the last NON-ZERO digit of a 
  14. >given factorial.  For instance,
  15.  
  16. >5! = 120
  17.  
  18. >so the last non-zero digit is 2.  I want to be able to do this up to 1000.  
  19. >Problem is, no matter how huge of a data-type you use, there isn't any way for 
  20. >the computer to handle 1000!, it is however possible to find the last non-zero 
  21. >digit somehow.  One thing I have tried is as I went through mulitplying the 
  22. >series of numbers in the factorial (5 * 4 * 3 * 2) I would remove all the 
  23. >trailing ZEROS, I got this to work up to 789, but it wont work with 1000 and i 
  24. >am not really sure why.  If anyone has a clue how I can do this let me know.
  25.  
  26. >-- 
  27. >The Crow - thecrow@iconn.net
  28. >"It can't rain all the time"
  29. >-Kryptology
  30.  
  31. program TryThis(input,output);
  32.  
  33.    function RMNZD(
  34.       n                        : integer
  35.        )                       : integer;
  36.    { determine the right most non-zero digit } begin
  37.       while n mod 10 = 0 do
  38.          n:=n div 10;
  39.       RMNZD:=N mod 10;
  40.        end { RMNZD };
  41.  
  42. var
  43.    digit : byte;
  44.    i     : integer;
  45.    n     : integer;
  46. { } begin
  47.    readln(input,n);
  48.    if n > 1 then { only treat n > 1 } begin
  49.       digit:=1;
  50.       for i:=2 to n do
  51.          digit:=RMNZD(digit)*RMNZD(i);
  52.       writeln(output,RMNZD(digit));
  53.        end;
  54.     end { TryThis }.
  55.  
  56.